﻿namespace B10.Controllers
{
    using System;
    using System.Linq;
    using System.Web.Mvc;
    using B10.Models;
    using B10.Helpers;
    using System.Linq.Expressions;

    [HandleError]
    public class HomeController : Controller
    {
        IDatabaseRepository _db;

        public HomeController()
            : this(new DatabaseRepository())
        { }

        public HomeController(IDatabaseRepository db)
        {
            _db = db;
        }

        public ActionResult PostOnWall()
        {
            Facebook.FacebookConnect.PublishToWall();
            return null;
        }

        //[OutputCache(Duration = 60, VaryByParam = "page,sort")]
        public ActionResult Index(int? page, PagesSortOrder? sort)
        {
            const int pageSize = 15;
            var pages = _db.GetActivePages();
            

            var paginated = new PaginatedList<UserPage>(
                GetOrderedPages(pages, sort), page ?? 1, pageSize);

            return View(new PageListViewModel(paginated/*, GetAdditionalPages(pages)*/));
        }

        //[OutputCache(Duration = 60, VaryByParam = "category,page,sort")]
        public ActionResult Categories(string categoryName, Categories? category, int? page,
            PagesSortOrder? sort)
        {
            if (category.HasValue)
            {
                string item = CategoryHelper.GetCategoryName(category.Value);
                if (item != categoryName)
                    return RedirectToAction("Categories", new { categoryName = item, category = (int)category.Value, page = page });
            }
            const int pageSize = 15;
            var pages = _db.GetActivePages();
            if (category.HasValue)
            {
                pages = pages.Where(p => p.Category == category.Value
                    || p.Category2 == category.Value
                    || p.Category3 == category.Value);
            }

            var paginated = new PaginatedList<UserPage>(
                GetOrderedPages(pages, sort), page ?? 1, pageSize);
            return View("Index", new PageListViewModel(paginated/*, GetAdditionalPages(pages)*/));
        }

        //[OutputCache(Duration = 60, VaryByParam = "id")]
        public ActionResult Page(string pageUrl, int id)
        {
            if (String.IsNullOrEmpty(pageUrl))
                return RedirectToAction("Index");
            UserPage page = _db.GetPage(id);
            if (page == null)
                return new FileNotFoundResult();
            if (page.Status != PageStatus.Accepted && (!Request.IsAuthenticated
                 || !page.IsOwner(UserHelper.UserId)))
                return new ForbiddenResult();
            return View(new UserPageViewModel(page/*, profile, GetAdditionalPages(pages)*/));
        }


        private IQueryable<UserPage> GetOrderedPages(IQueryable<UserPage> list,
            PagesSortOrder? sortOrder)
        {
            if (!sortOrder.HasValue)
                return list.OrderByDescending(p => p.PageViews);
            switch (sortOrder)
            {
                case PagesSortOrder.Newest:
                    return list.OrderByDescending(p => p.PublishDate);
                case PagesSortOrder.TopAll:
                    return list.OrderByDescending(p => p.PageViews);
                case PagesSortOrder.TopDay:
                    return list.OrderByDescending(p => p.PageViewsDay);
                case PagesSortOrder.TopMonth:
                    return list.OrderByDescending(p => p.PageViewsMonth);
                case PagesSortOrder.TopWeek:
                    return list.OrderByDescending(p => p.PageViewsWeek);
                default:
                    return list.OrderByDescending(p => p.PageViews);
            }
        }

        //private IQueryable<UserPage> GetAdditionalPages(IQueryable<UserPage> list)
        //{
        //    return list.OrderByDescending(p => p.PublishDate).Take(7);
        //}

        //private IQueryable<UserPage> GetAdditionalPages(UserPage page)
        //{
        //    var pages = _db.GetActivePages();
        //    pages = pages.Where(p => p.Category == page.Category
        //        || p.Category == page.Category2 || p.Category == page.Category3
        //        || p.Category2 == page.Category || p.Category3 == page.Category
        //        || p.Category2 == page.Category2 || p.Category2 == page.Category3
        //        || p.Category3 == page.Category3 || p.Category3 == page.Category2);

        //    return GetAdditionalPages(pages);
        //}
    }
}
